package com.qingfeng.client.service;

import com.qingfeng.client.dao.SellerBillMapper;
import com.qingfeng.client.dao.TradeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.TimeZone;

/**
 * 账单相关
 * 即：收入，提现
 */
@Service
public class BillService {

    @Resource
    private TradeMapper tradeDao;

    @Resource
    private SellerBillMapper sellerBillDao;



    /**
     * 最新剩余金额
     * @param storeId
     * @return
     */
    public int totalMoney(Long storeId){
        Integer sum = sellerBillDao.totalMoney(storeId);
        if (sum == null) {
            return 0;
        }else {
            return sum;
        }
    }

    /**
     * 获取几天的收入总和
     * @param storeId
     * @return
     */
    public int getTotalIncom(Long storeId,int day){
        Long current = System.currentTimeMillis();
        Long startTime = current / 1000 - day * 24 * 60 * 60;
        Integer sum = sellerBillDao.getTotalIncom(storeId,startTime);
        if (sum == null) {
            return 0;
        }else {
            return sum;
        }
    }


    /**
     * 获取交易中的总金额
     * @param storeId
     * @return
     */
    public int getInTradeMoney(Long storeId) {
        Integer sum = tradeDao.getInTradeMoney(storeId);
        if (sum == null) {
            return 0;
        } else {
            return sum;
        }
    }

    /**
     * 已提现金额
     * @param storeId
     * @return
     */
    public int getWithDrawCashMoney(Long storeId) {
        Integer sum = sellerBillDao.getWithDrawCashMoney(storeId);
        if (sum == null) {
            return 0;
        } else {
            return sum;
        }
    }





    /**
     * 获取收入，和其对应的日期
     * @param storeId
     * @param day     间隔天数,7天，或者30天
     * @return
     */
    public List getTradeFinishList(Long storeId, int day) {
        Long current = System.currentTimeMillis();
        long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
        Long time = zero / 1000 - day * 24 * 60 * 60;
        List list = sellerBillDao.getBillByDay(storeId, time);
        return list;
    }

    /**
     * 获取提现详情
     * @param storeId
     * @return
     */
    public List getWithDrawList(Long storeId) {
        return sellerBillDao.getWithDrawList(storeId);
    }


}
